﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace CsharpWpfManual
{
    class webapi
    {
        public void webclienthelper()
        {
            WebClient webClient = new WebClient();
        }

        public static string HttpGet(string url)
        {
            //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.Accept = "text/html, application/xhtml+xml, */*";
            request.ContentType = "application/json";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }


        public static void  DownLoadFile(string url, DownFileInfo downFileInfo)
        {
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(downFileInfo);

            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "Post";
            request.Accept = "text/html, application/x-msdownload, */*";
            request.ContentType = "application/json";
            byte[] btBodys = Encoding.UTF8.GetBytes(json);
            request.GetRequestStream().Write(btBodys, 0, btBodys.Length);
            

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if(response.StatusCode== HttpStatusCode.OK)
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (Stream stream = new FileStream(downFileInfo.FileName, FileMode.Create))
                    {
                        byte[] bArr = new byte[1024];
                        int size;
                        while ((size = responseStream.Read(bArr, 0, bArr.Length)) > 0)
                        {
                            stream.Write(bArr, 0, size);
                        }
                    }
                }
            }

        }


        public static void  UpdateLoadFile(string url,string  filepath)
        {
            if (!File.Exists(filepath))
                return;
            string filename = Path.GetFileName(filepath);
            WebClient webClient = new WebClient();
            webClient.Credentials = CredentialCache.DefaultCredentials;
            webClient.Headers.Add("Content-Type", "application/form-data");//注意头部必须是form-data
            webClient.QueryString["fname"]= filename;
            byte[] fileb=webClient.UploadFile(url, "Post", filepath);
            string response = Encoding.UTF8.GetString(fileb);
            Console.WriteLine(response);

        }
    }

    public class DownFileInfo
    {
        public string UserID { get; set; }
        public string Passwd { get; set; }
        public string FileName { get; set; }
    }
}
